#!/usr/bin/envpython
# -*-coding:UTF-8-*-
'''
@File    :   request_validation_error_helper.py
@Contact :   308711822@qq.com
@License :   (C) Copyright 2021-2225, Personal exclusive right.

@Modify Time      @Author    @Version    @Desciption
------------      -------    --------    -----------
2023/2/6 20:59   小钟同学      1.0         None
'''
import warnings
import typing


def validation_error(_request, exc):
    curr_model = None
    if hasattr(_request.state, 'parameter_model'):
        curr_model = _request.state.parameter_model
        for err in exc.errors():
            # 获取错误字段名称
            print(":错误字段名称:", err["loc"][1])
            #  获取错误的类型
            print(":错误的类型:", err["type"])
            # err["msg"]  配置错误信息
            print(":错误信息:", err["msg"])
            # 修改错误信息为模型对应的中文错误信息
            try:
                loction = err["loc"][0]
                name = err["loc"][1]
                _value = err['ctx']

                if loction in ['body', 'get']:
                    limit_value = None
                    if 'limit_value' in _value:
                        limit_value = _value['limit_value']
                    if limit_value:
                        err["msg"] = curr_model.Config.error_msg[name][err["type"]].format(name=name,
                                                                                           limit_value=limit_value)
                    else:
                        err["msg"] = curr_model.Config.error_msg[name][err["type"]].format(name=name)
            except KeyError as e:
                warnings.warn(f'{err["loc"][1]}未定义错误模板信息', Warning)
                curr_model = None
    return curr_model
